﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYTeeFittingConnect : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException("commandData", "is null");
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				List<Pipe> list = new List<Pipe>();
				IList<XYZ> list2 = new List<XYZ>();
				ISelectionFilter selectionFilter = new PipeSelectionFilter();
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				ILayForm layForm = new ILayForm(document);
				FamilySymbol familySymbol = layForm.LayPipeFittingFormShow(document, "水管三通");
				if (familySymbol == null)
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					string text = "三通连接";
					if (layForm.pipeFittingFlag == 1)
					{
						activeUIDocument.PromptForFamilyInstancePlacement(familySymbol);
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
					else
					{
						for (;;)
						{
							transaction = new Transaction(document, text);
							transaction.Start();
							list.Clear();
							list2.Clear();
							RevitVersionFuncs.ClearSelection(selection);
							Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择入口水管");
							list.Add(document.GetElement(reference) as Pipe);
							list2.Add(reference.GlobalPoint);
							reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择出口水管");
							list.Add(document.GetElement(reference) as Pipe);
							list2.Add(reference.GlobalPoint);
							reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择支管");
							list.Add(document.GetElement(reference) as Pipe);
							list2.Add(reference.GlobalPoint);
							string text2 = "";
							string familySymbolName = new ConnectPipeManage().GetFamilySymbolName(ConnectPipeStyle.Tee);
							if (hypipeConnect.TeeFittingConnPipe(list, familySymbolName, ref text2) == null)
							{
								MessageBox.Show(text2, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							}
							else
							{
								document.Regenerate();
							}
							transaction.Commit();
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
